package cn.pegasus.teamwork.pegasus.controller;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.iyizhan.teamwork.base.util.PageObject;
import cn.iyizhan.teamwork.base.util.ParameterEunm;
import cn.iyizhan.teamwork.base.util.ReMessage;
import cn.pegasus.teamwork.manager.controller.BaseManagerController;
import cn.pegasus.teamwork.pegasus.entity.BussEquipmentInfo;
import cn.pegasus.teamwork.pegasus.mapper.BussEquipmentInfoMapper;
import cn.pegasus.teamwork.pegasus.service.BussEquipmentInfoService;

/**
 * 设备
 * @author 40854
 *
 */
@Controller
@RequestMapping("/equipment")
public class EquipmentController extends BaseManagerController<BussEquipmentInfo, BussEquipmentInfoMapper>{

	private static final Logger logger = LoggerFactory.getLogger(EquipmentController.class);
	
	@Autowired
	private BussEquipmentInfoService equipmentInfoService;
	/**
	 * 查询设备列表
	 * isPage  1 不用分页
	 */
	@RequestMapping(value = "/list",method = {RequestMethod.GET})
	 public ModelAndView list(@RequestParam Map<String, Object> map){
		 ModelAndView view = new ModelAndView("equipment/equipment-list");
		 if(map.isEmpty()){
			map.put("pageData", 10);
			map.put("curPage", 1);
		}
		 try{
			 map.put("currType", 1);
			 PageObject<BussEquipmentInfo> dicList = equipmentInfoService.PageList(map);
			 view.addObject("list", dicList);
			 view.addObject("map", map);
		 }catch(Exception e){
			 logger.error("查询设备列表信息异常",e.getMessage());
		 }
		 return view;
	 }
	
	 /**
	  * 跳转新增页面
	  * type  1  编辑
	  */
	 @RequestMapping(value="/goAdd" ,method = {RequestMethod.GET})
	 public ModelAndView goAdd(@RequestParam(value="type",required = false) String type,
			 @RequestParam(value="id",required = false) Integer id){
		 if(null == type){
			 ModelAndView view = new ModelAndView("equipment/equipment-add");
			 return view;
		 }else{
			 ModelAndView view = new ModelAndView("equipment/equipment-edit");
			 BussEquipmentInfo info = equipmentInfoService.findEqInfo(id);
			 view.addObject("info", info);
			 return view;
		 }
	 }
	 /**
	  * 新增
	  */
	 @ResponseBody
	 @RequestMapping(value = "/add")
	 public Map<String, Object> add(@RequestParam Map<String, Object> map){
		Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
        	equipmentInfoService.addEq(getAdmin().getId(), map);
            resultMap = ReMessage.resultBack(ParameterEunm.SUCCESSFUL_CODE, null);
        } catch (Exception e) {
            logger.error("新增设备信息异常", e);
            resultMap = ReMessage.resultBack(ParameterEunm.ERROR_500_CODE, null);
        }
        return resultMap;
	 }
	 
	 /**
	  * 修改信息
	  * @param map
	  * @return
	  */
	 @ResponseBody
	 @RequestMapping(value="/update")
	 public Map<String,Object> update(@RequestParam Map<String, Object> map){
		Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
        	equipmentInfoService.updateEq(getAdmin().getId(), map);
            resultMap = ReMessage.resultBack(ParameterEunm.SUCCESSFUL_CODE, null);
        } catch (Exception e) {
            logger.error("修改设备信息异常", e);
            resultMap = ReMessage.resultBack(ParameterEunm.ERROR_500_CODE, null);
        }
        return resultMap;
	 }
	 
	 /**
	  * 删除
	  */
	 @ResponseBody
	 @RequestMapping(value="/delete")
	 public Map<String,Object> delete(@RequestParam(value="id",required = false) Integer id){
		Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
        	equipmentInfoService.delete(getAdmin().getId(),id);
        	resultMap = ReMessage.resultBack(ParameterEunm.SUCCESSFUL_CODE, null);
        } catch (Exception e) {
            logger.error("删除设备信息异常", e);
            resultMap = ReMessage.resultBack(ParameterEunm.ERROR_500_CODE, null);
        }
        return resultMap;
	 }
}
